package net.caixiaomi.info.ui.football;

import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.PlayFootballItemChangeListener;
import net.caixiaomi.info.logger.Timber;
import net.caixiaomi.info.model.FootBallHeaderModel;
import net.caixiaomi.info.util.Constants;

import java.util.List;

/**
 * 足球玩法
 */

public class FootballAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private ItemViewProxy mItemViewProxy;

    private PlayFootballItemChangeListener mListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public FootballAdapter(int type, List<MultiItemEntity> data) {
        super(data);
        mItemViewProxy = FootballAdapterFactory.createItemViewProxy(type, this);
        mItemViewProxy.addItemType();
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case Constants.ITEM_VIEW_HEADER:
                final FootBallHeaderModel model = (FootBallHeaderModel) item;
                helper.setGone(R.id.image, model.isHot());
                helper.setText(R.id.title, model.getTitle());
                helper.setTextColor(R.id.title, ContextCompat.getColor(CommonApp.getInstance(), model.isHot() ? R.color.orange_primary : R.color.secondary_text));
                ImageView btnExpand = helper.getView(R.id.btn_expand);
                btnExpand.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int adapterPosition = helper.getAdapterPosition();
                        if (model.isExpanded()) {
                            collapse(adapterPosition, false, true);
//                            rotationExpandIcon(v, -180, 0);
                        } else {
//                            rotationExpandIcon(v, 0, -180);
                            expand(adapterPosition, false, true);
                        }

                    }
                });
                helper.setImageResource(R.id.btn_expand, model.isExpanded() ? R.mipmap.ic_expand_less : R.mipmap.ic_expand_more);
                break;
            case Constants.ITEM_VIEW_ITEM:
                mItemViewProxy.bindView(helper, item);
                break;
        }

    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void rotationExpandIcon(final View view, float from, float to) {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(from, to);//属性动画
        valueAnimator.setDuration(300);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Timber.d("animation.getAnimatedValue() == " + animation.getAnimatedValue());
                view.setRotation((Float) animation.getAnimatedValue());
            }
        });
        valueAnimator.start();
    }

    public PlayFootballItemChangeListener getListener() {
        return mListener;
    }

    public void setListener(PlayFootballItemChangeListener listener) {
        this.mListener = listener;
    }
}
